﻿@page "/t2"
@using System.Diagnostics.CodeAnalysis

<Select Items="Items" TValue="string" OnSelectedItemChanged="OnSelectedItemChanged" />
<Button OnClick="OnClick" Text="动态数据表格"></Button>

@if (DynamicColumnList != null)
{
    <table>
        <tr>
            @foreach (var element in DynamicColumnList)
            {
                <th>@element</th>
            }
        </tr>
    </table>
}
@if (DynamicColumnList != null)
{
    <Table TItem="CustomDynamicData" OnQueryAsync="OnQueryAsync"
    @ref="dgList"
    IsAutoQueryFirstRender="false"    
    IsStriped="true" IsBordered="true" ShowToolbar="true" ShowColumnList="true" ShowDefaultButtons="false" ShowRefresh="false">
        <TableColumns>
            @foreach (var element in DynamicColumnList)
            {
                <TableColumn Field="@element.ToString()" FieldName="@element.ToString()" Text="@element" Sortable="true" 
                Filterable="true">
                    <Template Context="v">
                        <div> @v.Value</div>
                    </Template>
                </TableColumn>
            }
        </TableColumns>
    </Table>
}

@code {
    Table<CustomDynamicData>? dgList{ get; set; }

    [NotNull]
    private List<string>? DynamicColumnList { get; set; }

    public List<SelectedItem>? Items{ get; set; }

    /// <summary>
    /// OnInitialized
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();

        Items = new List<SelectedItem>()
        {
            new SelectedItem("A","中国"),
            new SelectedItem("B","韩国"),
            new SelectedItem("C","美国"),
        };
        Items.Insert(0, new SelectedItem("", "请选择"));

        // 构造动态列
        // var now = DateTime.Now;
        // DynamicColumnList = Enumerable.Range(1, 5).Select(index => now.AddMinutes(-1 * index).ToString("HH:mm")).ToList();
    }

    private readonly static Random random = new();

    async Task OnClick()
    {
        await dgList!.QueryAsync();
    }

    int count;

    Task OnSelectedItemChanged(SelectedItem item)
    {
        if (item.Value == "")
            return Task.CompletedTask;
        var now = DateTime.Now;

        if (item.Value == "A")
        {
            count = 5;
            DynamicColumnList = Enumerable.Range(1, 5).Select(index => "中国" + now.AddMinutes(-1 * index).ToString("HH:mm")).ToList();
        }
        else if (item.Value == "B")
        {
            count = 3;
            DynamicColumnList = Enumerable.Range(1, 3).Select(index => "韩国" + now.AddMinutes(-1 * index).ToString("HH:mm")).ToList();
        }
        else if (item.Value == "C")
        {
            count = 8;
            DynamicColumnList = Enumerable.Range(1, 8).Select(index => "美国" + now.AddMinutes(-1 * index).ToString("HH:mm")).ToList();
        }
        StateHasChanged();

        return Task.CompletedTask;
    }

    private Task<QueryData<CustomDynamicData>> OnQueryAsync(QueryPageOptions options)
    {
        var items = Enumerable
                .Range(1, DynamicColumnList.Count)
                .Select(index => new CustomDynamicData(index.ToString()
                , DynamicColumnList.ToDictionary(d => d.ToString(), d => $"{random.Next(1000, 9999)}")));
        
        return Task.FromResult(new QueryData<CustomDynamicData>() { Items = items, TotalCount = items.Count(), IsSorted = true, IsFiltered = true });
    }
}
